package com.geaosu.wanandroid.kotlin.learn

/*
2. when条件语句:
就是java中的switch语句, when语句必须掌握, 常用功能
特性:
    1. 有返回值
    2. 没有break结束
    3. 任意类型参数


 */
fun main() {
//    val s = getSource("tom")
//    println(s)

//    val s = getSource2("bob")
//    println(s)

//    getSource3(10F)

    val v = getSource4("tomsjdlfkj")
    println(v)
}

// 查询成绩
fun getSource(name: String) = if (name == "tom") {
    100
} else if (name == "jok") {
    90
} else if (name == "bob") {
    10
} else {
    0
}

// when 精确匹配
fun getSource2(name: String) = when (name) {
    "tom" -> 100
    "jok" -> 90
    "bob" -> 10
    else -> 0
}

// when 类型匹配
fun getSource3(name: Number) = when (name) {
    is Int -> println("num is Int")
    is Double -> println("num is Double")
    else -> println("num not is a Number")
}

// when 不带参数的用法
fun getSource4(name: String) = when {
    name.startsWith("t") -> 80
    name == "tom" -> 100
    name == "jok" -> 90
    name == "bob" -> 10
    else -> 0
}